﻿using System;
using System.Windows.Forms;
using System.Xml;

using ClassLibrary;

namespace TestProject
{
    public partial class NewGene : Form
    {
        private GeneTable gen_t;
        public NewGene(GeneTable g)
        {
            InitializeComponent();
            gen_t = g;
        }

        private void canc_butt_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ok_butt_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(gen_t.path));
            foreach (XmlNode node in doc.GetElementsByTagName("gene"))
            {
                if (node.Attributes[1].Value == name_box.Text)
                {
                    MessageBox.Show("Gene with this name already exists.\nPlease choose another name.");
                    return;
                }
            }
            XmlElement el = doc.CreateElement("gene");
            el.SetAttribute("Id", Functions.set_id(gen_t).ToString());
            el.SetAttribute("Name", name_box.Text);
            el.SetAttribute("AmountAlells", amount_box.Value.ToString());
            doc.GetElementsByTagName("genes")[0].AppendChild(el);
            doc.DocumentElement.Attributes[1].Value = (Convert.ToInt32(doc.DocumentElement.Attributes[1].Value) + 1).ToString();
            doc.Save(gen_t.path);
            this.Close();
        }
    }
}
